﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Collections.Specialized;
using System.IO;

namespace braklimitu.App_Code
{
    public enum EgofilesStatus
    {
        OK,
        DELETED
    }
    public class EgofilesAgent : Agent
    {
        private static string LoginUrl = "http://egofiles.com/ajax/register.php";
        private static string LogoutUrl = "http://egofiles.com/logout";
        protected String Url { get; set; }
        protected String Username { get; set; }
        protected String Password { get; set; }
        protected String DirectUrl { get; set; }
        public String ResponseLogin { get; set; }
        protected CookieCollection cookie;
        protected long FileSize { get; set; }
        protected String FileName { get; set; }
        private EgofilesAgent status;
        public EgofilesAgent Status { get { return status; } }
        public EgofilesAgent(String Url, String Username, String Password)
        {
            this.Url = Url;
            this.Username = Username;
            this.Password = Password;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
        }

        public EgofilesAgent(String Url)
        {
            this.Url = Url;
            this.DirectUrl = String.Empty;
            this.FileSize = 0;
        }

        public override bool LogIn()
        {
            string postData = "log=1&loginV=" + Username + "&passV=" + Password;
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(LoginUrl);
            
            webRequest.CookieContainer = new CookieContainer();
            webRequest.Method = "POST";
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.ContentLength = postData.Length;
            
            using (StreamWriter requestWriter2 = new StreamWriter(webRequest.GetRequestStream()))
            {
                requestWriter2.Write(postData);
            }
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            string responseData = string.Empty;

            using (StreamReader responseReader = new StreamReader(webRequest.GetResponse().GetResponseStream()))
            {
                responseData = responseReader.ReadToEnd();
            }
            cookie = response.Cookies;
            ResponseLogin = responseData;
            foreach (Cookie c in cookie)
            {
                System.Diagnostics.Debug.WriteLine(c.Name + ":" + c.Value);
                if (c.Value == "deleted")
                    return false;
            }
            if (responseData.ToString().Contains("ok"))
                return true;
            return false;
        }

        public override bool LogOut()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(LogoutUrl);
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            return true;
        }

        public override DownloadLink GetFileInfo()
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);
            webRequest.CookieContainer = new CookieContainer();
            webRequest.CookieContainer.Add(cookie);
            
            HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();
            
            DownloadLink url = new DownloadLink(response.ResponseUri.OriginalString);
            url.Length = response.ContentLength;
            url.Name = response.ResponseUri.Segments[response.ResponseUri.Segments.Length - 1];
            return url;
        }

    }

}